package com.crealth.handler;

import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.ConsumerProductDb;
import com.crealth.db.SendBackEarnestDb;
import com.crealth.memcached.UserCached;
import com.crealth.model.ConsumerProductModel;
import com.crealth.model.SendBackEarnestModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class SendBackEarnestHandler {

	public static NetMessageData obtainBackEarnestInfo(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int productID = msgJson.getIntValue("productID");
		SendBackEarnestModel earnest = SendBackEarnestDb.selectEarnestByProductID(productID);
		if (earnest == null) {
			return null;
		}
		
		JSONObject resJson = new JSONObject();	
		resJson.put("resCode", 0);
		resJson.put("earnestInfo", earnest.toJSONObject());
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData saveBackEarnestInfo(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		SendBackEarnestModel earnest = new SendBackEarnestModel();
		earnest.parseFromJson(msgJson);
		SendBackEarnestModel existEarnest = SendBackEarnestDb.selectEarnestByProductID(productID);
		if (existEarnest == null) {			
			SendBackEarnestDb.insertSendBackEarnest(earnest);
		}
		else {
			SendBackEarnestDb.updateEarnestByProductID(earnest);
		}
		String csMemo = msgJson.getString("csMemo");
		ConsumerProductDb.updateCustomerServiceMemoByID(productID, csMemo);
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_START_BACK);
		
		JSONObject resJson = new JSONObject();
		resJson.put("resCode", 0);
		resJson.put("earnestInfo", earnest.toJSONObject());
		resJson.put("opUser", userID);
		resJson.put("productID", productID);
		resJson.put("csMemo", csMemo);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		sendMsg.setMsgCmd(revData.getMsgCmd());
		if (existProduct != null) {
			int companyID = existProduct.getCompanyID();
			ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
			ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
			ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
			ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		}
		
		return sendMsg;
	}
	
	//  分公司经理   确认退款
	public static NetMessageData checkBackEarnestInfo(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Branch_Mgr_Fu) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());			
			return sendMsg;
		}
		
		String csMemo = msgJson.getString("csMemo");
		ConsumerProductDb.updateCustomerServiceMemoByID(productID, csMemo);
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_DaiHe);
				
		resJson.put("resCode", 0);			
		resJson.put("productID", productID);
		resJson.put("opUser", userID);
		resJson.put("csMemo", csMemo);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		int companyID = existProduct.getCompanyID();
		sendMsg.setMsgCmd(revData.getMsgCmd());		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	//  财务  退款
	public static NetMessageData financeBackEarnest(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}
		JSONObject resJson = new JSONObject();
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Financer) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());			
			return sendMsg;
		}
		
		String csMemo = msgJson.getString("csMemo");
		ConsumerProductDb.updateCustomerServiceMemoByID(productID, csMemo);
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_DaiTui);
							
		resJson.put("resCode", 0);			
		resJson.put("productID", productID);
		resJson.put("opUser", userID);
		resJson.put("csMemo", csMemo);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		int companyID = existProduct.getCompanyID();
		sendMsg.setMsgCmd(revData.getMsgCmd());		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	//  客服  核实 退款是否已经到了客户账上
	public static NetMessageData confirmBackEarnest(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}		
		if (!msgJson.containsKey("sbEarnestID")) {
			return null;
		}
		int sbEarnestID = msgJson.getIntValue("sbEarnestID");
		String checkerName = msgJson.getString("checkerName");
		SendBackEarnestDb.updateEarnestCheckNameByID(sbEarnestID, checkerName);
		
		String csMemo = msgJson.getString("csMemo");
		ConsumerProductDb.updateCustomerServiceMemoByID(productID, csMemo);
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_TK_ALREADY);
		
		JSONObject resJson = new JSONObject();
		resJson.put("resCode", 0);			
		resJson.put("productID", productID);
		resJson.put("opUser", userID);
		resJson.put("sbEarnestID", sbEarnestID);
		resJson.put("checkerName", checkerName);
		resJson.put("csMemo", csMemo);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		int companyID = existProduct.getCompanyID();
		sendMsg.setMsgCmd(revData.getMsgCmd());		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	//  撤销 退款
	public static NetMessageData cancelBackEarnest(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);
		int productID = msgJson.getIntValue("productID");
		ConsumerProductModel existProduct = ConsumerProductDb.selectConsumerProductByID(productID);
		if (existProduct == null) {
			return null;
		}
		
		ConsumerProductDb.updateEarnestStatusByID(productID, ConsumerProductModel.DJ_BACK_CANCEL);
		
		JSONObject resJson = new JSONObject();
		resJson.put("resCode", 0);
		resJson.put("productID", productID);
		resJson.put("opUser", userID);		
		sendMsg.setMsgBody(resJson.toJSONString());
		
		int companyID = existProduct.getCompanyID();
		sendMsg.setMsgCmd(revData.getMsgCmd());		
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
}
